package com.itany.intercepter;

import com.alibaba.fastjson.JSON;
import com.itany.ApplicationContextHolder;
import com.itany.constant.StatusConst;
import com.itany.pojo.ManagerUser;
import com.itany.pojo.Permission;
import com.itany.service.PermissionService;
import com.itany.vo.ActionResult;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class PermissionInterceptor implements HandlerInterceptor {

    PermissionService service = ApplicationContextHolder.getBean(PermissionService.class);
    List<Permission> allPermissions = service.findAllModulesWithPermission();

    List<Permission> loginUserPermissions = new ArrayList<>();

    Set<String> allowedUrls = new HashSet<>();

    {
        allowedUrls.add("/manager/showLogin");
        allowedUrls.add("/manager/managerUser/login");
        allowedUrls.add("/manager/showIndex");
        allowedUrls.add("/manager/managerUser/logout");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        HttpSession session = request.getSession();
        String requestURI = request.getRequestURI();

        System.out.println("拦截器 :" + requestURI);
        if (allowedUrls.contains(requestURI)) {
            return true;
        } else {
            try {
                ActionResult result = new ActionResult();
                result.setStatus(StatusConst.FAIL_RESPONSE);
                result.setMsg("no permission没有乱码");
                String string = JSON.toJSONString(result);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().print(string);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        HttpSession session = request.getSession();
        String requestURI = request.getRequestURI();
        if (requestURI.equals("/manager/managerUser/login")) {
            ManagerUser managerUser = (ManagerUser) session.getAttribute(StatusConst.LOGIN_USER);
            if (managerUser != null) {
                //添加所有权限
                List<Permission> modules = service.findAllModulesWithPermissionsByLoginUserId(managerUser.getId());
                modules.forEach(module -> {
                    module.getPermissions().forEach(permission -> {
                        loginUserPermissions.add(permission);
                        allowedUrls.add("/manager" + permission.getUrl());
                    });
                });
            }
        }

        if (requestURI.equals("/manager/managerUser/logout")) {
            ManagerUser managerUser = (ManagerUser) session.getAttribute(StatusConst.LOGIN_USER);
            if (managerUser != null) {
                //添加所有权限
                loginUserPermissions.forEach(permission -> {
                    allowedUrls.remove("/manager" + permission.getUrl());
                });


            }
        }


    }
}
